package vivace.view;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.sound.midi.*;
import vivace.model.*;
import java.util.Vector;

import lib.Komponenter.CTable;
import lib.Texter.*;


public class TracksFrame extends JInternalFrame {
	private Sequencer model;
	
	public TracksFrame(Sequencer model) {
		this.model = model;
		
		TracksListPanel trackslist = new TracksListPanel();
		TracksContentPanel trackscontent = new TracksContentPanel();
		
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				trackslist, trackscontent);
		
		splitPane.setContinuousLayout(true);
		
		Container contentPane = this.getContentPane();
		
		contentPane.setLayout(new BorderLayout());
		
		contentPane.add(splitPane, BorderLayout.CENTER);
		this.pack();
		
	}
	
	private class TracksListPanel extends JScrollPane {
		public TracksListPanel() {
			Vector<String> columnNames = new Vector<String>();
			columnNames.add("#");
			columnNames.add(Text.getText("NAME"));
			columnNames.add(Text.getText("CHANNEL"));
			columnNames.add(Text.getText("VOICE"));
			columnNames.add(Text.getText("SOLO"));
			columnNames.add(Text.getText("MUTE"));
			columnNames.add(Text.getText("VOLUME"));
			columnNames.add(Text.getText("PAN"));
			
			Vector<Vector> rows = new Vector<Vector>();
			
			Track[] tracks = model.getSequence().getTracks();
	
		
			for (int i = 0; i < tracks.length; i++) {
				Track t = tracks[i];
				
				/* initialize vector */
				Vector row = new Vector();
				
				/* add track no */
				row.add(i + 1);
				
				/* get track name (meta message) */
				// TODO: put in separate VTrack class, e.g. vtrack.getName()?
				MidiMessage mm = t.get(0).getMessage();
				
				if(mm instanceof MetaMessage) {
					MetaMessage meta = (MetaMessage) mm;
					
					// test if track name meta message, type 0x03
					if (meta.getType() == 0x03) {
						row.add(new String(meta.getMessage()));
					} else {
						row.add("");
					}
					
					// TODO: what to do if meta message not found?
				} else {
					row.add("");
				}
				
				/* get track channel */
				row.add("");
				/* get track voice */
				row.add("");
				
				/* get track solo status */
				if (model.getTrackSolo(i)) {
					row.add("S+");
				} else {
					row.add("S-");
				}
				
				/* get track mute status */
				if (model.getTrackMute(i)) {
					row.add("M+");
				} else {
					row.add("M-");
				}
				
				/* get track volume */
				
				/* add row to row vector */
				rows.add(row);
			}
			
			DefaultTableModel dataModel = new DefaultTableModel(rows, columnNames);
			
			CTable tracksTable = new CTable(dataModel);
			
			tracksTable.setRowHeight(20);
			tracksTable.setFillsViewportHeight(true);
			
			// set 1st column (#) to a fixed width and prevent resizing and editing
			TableColumn firstColumn = tracksTable.getColumnModel().getColumn(0);
			firstColumn.setPreferredWidth(35);
			firstColumn.setResizable(false);
			
			this.setViewportView(tracksTable);
			this.setVerticalScrollBarPolicy(ScrollPaneLayout.VERTICAL_SCROLLBAR_AS_NEEDED);
			this.setHorizontalScrollBarPolicy(ScrollPaneLayout.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			
		}
	}
	
	private class TracksContentPanel extends JPanel {
		public TracksContentPanel() {
		
		}
	}
}
